/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snails;

import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.effect.ParticleEmitter;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import java.util.ArrayList;
import shooter.Main;
import shooter.WorldAppState;

/**
 *
 * @author Samuel
 */
public class Snail extends BetterCharacterControl
{
    int health = 1000;
    
    int turnRadius = 1;
    private boolean turningRight = false;
    private boolean turningLeft = false;
    private boolean moving = false;
    private String ID;
    
    private ArrayList<CannonBay> cannons;
    private CannonBay leftBay;
    private CannonBay rightBay;
    
    
    /**
     * creates snail
     * TODO: make collision mesh accurate to snail mesh
     * @param i
     * @param i0
     * @param i1
     * @param id 
     */
    public Snail(int i, int i0, int i1, String id)
    {
        super(i, i0, i1);
        ID = id;
        cannons = new ArrayList<CannonBay>();
    }
    
    /**
     * MUST be called after attaching to a node
     * 
     * TODO: switch this to an ovverwritten setSpatial()
     */
    public void init()
    {
        leftBay = new CannonBay(new Vector3f(6, 2, 0), new Vector3f(1, 0, 0), 10, 3, this);
        rightBay = new CannonBay(new Vector3f(-6, 2, 0), new Vector3f(-1, 0, 0), 10, 3, this);
        spatial.addControl(leftBay);
        spatial.addControl(rightBay);
        leftBay.initialize();
        rightBay.initialize();
        cannons.add(leftBay);
        cannons.add(rightBay);
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }
    
    
    
    
    
    public void fireLeft()
    {
        leftBay.broadside();
    }
    
    
    public void fireRight()
    {
        rightBay.broadside();
    }
    
    public void startTurningRight()
    {
        turningRight = true;
    }
    public void stopTurningRight()
    {
        turningRight = false;
    }
    
    public void startTurningLeft()
    {
        turningLeft = true;
    }
    public void stopTurningLeft()
    {
        turningLeft = false;
    }
    
    public void startMoving()
    {
        moving = true;
    }
    public void stopMoving()
    {
        moving = false;
        setWalkDirection(new Vector3f(0, 0, 0));
    }
    
    public void damage(int d)
    {
        health -= d;
        if(health <= 0)
        {
            spatial.getParent().detachChild(spatial);
            super.space.remove(super.rigidBody);
        }
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public int getTurnRadius() {
        return turnRadius;
    }

    public void setTurnRadius(int turnRadius) {
        this.turnRadius = turnRadius;
    }

    public ArrayList<CannonBay> getCannons() {
        return cannons;
    }

    public void setCannons(ArrayList<CannonBay> cannons) {
        this.cannons = cannons;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    @Override
    public void update(float tpf)
    {
        super.update(tpf);
        if(moving)
        {
            if(turningRight)
            {
                Quaternion rotator = new Quaternion();
                rotator.fromAngleAxis(FastMath.DEG_TO_RAD * turnRadius * (tpf * -40), Vector3f.UNIT_Y);
                setViewDirection(rotator.mult(getViewDirection()));
            }
            if(turningLeft)
            {
                Quaternion rotator = new Quaternion();
                rotator.fromAngleAxis(FastMath.DEG_TO_RAD * turnRadius * (tpf * 40), Vector3f.UNIT_Y);
                setViewDirection(rotator.mult(getViewDirection()));
            }
            setWalkDirection(getViewDirection().normalizeLocal().mult(30));
        }
    }
    
    /**
     * returns whether the objects are equal.
     * TODO: stop being lazy and define this based on something other than an arbitrary string.
     * @param o
     * @return 
     */
    @Override
    public boolean equals(Object o)
    {
        boolean equals = false;
        if(this.getClass().equals(o.getClass()))
        {
            Snail cp = (Snail) o;
            if(cp.getID().equals(getID()))
            {
                equals = true;
            }
        }
        
        
        return equals;
    }
}